package com.integral.xr.module.platform.service.pointsrule;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.integral.xr.module.platform.controller.admin.pointsrule.vo.*;
import com.integral.xr.module.platform.dal.dataobject.pointsrule.PointsRuleDO;
import com.integral.xr.framework.common.pojo.PageResult;
import com.integral.xr.framework.common.pojo.PageParam;
import com.integral.xr.framework.common.util.object.BeanUtils;

import com.integral.xr.module.platform.dal.mysql.pointsrule.PointsRuleMapper;

import static com.integral.xr.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.integral.xr.module.platform.enums.ErrorCodeConstants.*;

/**
 * 积分规则 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PointsRuleServiceImpl implements PointsRuleService {

    @Resource
    private PointsRuleMapper pointsRuleMapper;

    @Override
    public String createPointsRule(PointsRuleSaveReqVO createReqVO) {
        // 插入
        PointsRuleDO pointsRule = BeanUtils.toBean(createReqVO, PointsRuleDO.class);
        pointsRuleMapper.insert(pointsRule);
        // 返回
        return pointsRule.getId();
    }

    @Override
    public void updatePointsRule(PointsRuleSaveReqVO updateReqVO) {
        // 校验存在
        validatePointsRuleExists(updateReqVO.getId());
        // 更新
        PointsRuleDO updateObj = BeanUtils.toBean(updateReqVO, PointsRuleDO.class);
        pointsRuleMapper.updateById(updateObj);
    }

    @Override
    public void deletePointsRule(String id) {
        // 校验存在
        validatePointsRuleExists(id);
        // 删除
        pointsRuleMapper.deleteById(id);
    }

    private void validatePointsRuleExists(String id) {
        if (pointsRuleMapper.selectById(id) == null) {
            throw exception(POINTS_RULE_NOT_EXISTS);
        }
    }

    @Override
    public PointsRuleDO getPointsRule(String id) {
        return pointsRuleMapper.selectById(id);
    }

    @Override
    public PageResult<PointsRuleDO> getPointsRulePage(PointsRulePageReqVO pageReqVO) {
        return pointsRuleMapper.selectPage(pageReqVO);
    }

}